/*
 * Broom.cpp
 *
 *  Created on: Jul 13, 2009
 *      Author: scott
 */

#include "Broom.h"

Broom::Broom(World * world, SceneGraphNode * parent, bool ownsChildren):
	Weapon(parent, "media/broom.3ds", ownsChildren)
{
	this->world = world;

	setTranslation(irr::core::vector3df(-0.2f, -0.5f, 1.5f));
		setScale(irr::core::vector3df(0.001f, 0.001f, 0.001f));
		setRotation(irr::core::vector3df(-70.0f, 15.0f, 0.0f));

	// generate a body
	body = dBodyCreate(world->getWorld());
	dBodySetData(body, (void*) 1);
	dMass m;
	dMassSetSphere(&m, 10, 2);
	dMassAdjust(&m, 10);
	dBodySetMass(body,&m);
	sphere = dCreateSphere(0,1);
	dGeomSetBody(sphere, body);
	dBodySetPosition(body, 0, 0, 0);
	irr::core::matrix4 rot;
	rot.setRotationDegrees(getRotation());
	float rotBuffer[16];
	dBodySetRotation(body,(const dReal*) World::opengl2OdeR(rotBuffer, rot.pointer()));

}

Broom::~Broom() {

}

void Broom::switchTo()
{
	dSpaceAdd(world->getSpace(), sphere);
}

void Broom::use()
{

}

void Broom::switchFrom()
{
	dSpaceRemove(world->getSpace(), sphere);
}

void Broom::update()
{

	float xrot = parent->getParent()->getRotation().X;
	irr::core::vector3df rot = getRotation();
	rot.X = 270-xrot;
	if (rot.X > 180)
		rot.X -= 360;
	if (rot.X > 0)
		rot.X -= 180;

	//cout << "Rotation about X: " << rot.X << endl;
	setRotation(rot);
	updateAbsTransform();


	irr::core::vector3df pos = getAbsTranslation();
	dBodySetPosition(body, pos.X, pos.Y, pos.Z);
}

